#include "MusicBoard.h"
#include "EventReciever.h"

MusicBoard::MusicBoard(bool left, float x, float y)
{
	m_bPlaying=false;
	m_bLastPlaying=false;
	m_bPaused=false;
	m_bLooping=false;
	m_bLPF=false;
	m_bHPF=false;
	m_bMute=false;
	m_bLeft=left;
	IGUIButton* button;
	m_pMusic=NULL;
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_HIGHPASS,&m_pDsp1);
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_LOWPASS,&m_pDsp2);
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&m_pPitchShifter);
	m_pChannel->addDSP(m_pPitchShifter, 0);
	m_pChannel->addDSP(m_pDsp1, 0);
	m_pChannel->addDSP(m_pDsp2, 0);
	m_pDsp1->setBypass(true);
	m_pDsp2->setBypass(true);
	if(left)
	{
		m_pFileDisplay = TheEngine::Instance()->GetGUI()->addStaticText(m_wFile.c_str(),rect<s32>(60*x,316*y,240*x,316*y+8*y),true,true,0,1000,true);
		TheEngine::Instance()->GetGUI()->addButton(rect<s32>(75*x,328*y,225*x,328*y + 8*y), 0, 1000+MUSIC_LOAD_BUTTON,
			L"Load", L"Load Music #1");
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(230*x,328*y,250*x,328*y + 8*y),0,1000+MUSIC_MUTE_BUTTON,L"MUTE",L"Mutes track #1");
		button->setIsPushButton(true);
		m_pPlayButton=TheEngine::Instance()->GetGUI()->addButton(rect<s32>(50*x,340*y,250*x,340*y + 16*y), 0, 1000+MUSIC_PLAY_BUTTON,
			L"Play", L"Play/Pause Music #1");
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(75*x,360*y,225*x,360*y + 8*y), 0, 1000+MUSIC_LOOP_BUTTON,
			L"Loop", L"Loop this track");
		button->setIsPushButton(true);
		m_pSongPosition = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(50*x,376*y,250*x,376*y+8*y),0,1010);
		m_pSongPosition->setToolTipText(L"Song position");
		m_pSongPosition->setSmallStep(1);
		m_pSongPosition->setMin(0);
		m_pPanBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(50*x,388*y,250*x,388*y+6*y),0,1020);
		m_pPanBar->setToolTipText(L"Pan");
		m_pPanBar->setMin(-100);
		m_pPanBar->setMax(100);
		m_pPanBar->setPos(0);
		m_pPanBar->setSmallStep(5);
		m_pSpeedBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(50*x,398*y,250*x,398*y+4*y),0,1020);
		m_pSpeedBar->setToolTipText(L"Playback speed");
		m_pSpeedBar->setMin(1);
		m_pSpeedBar->setMax(200);
		m_pSpeedBar->setPos(100);
		m_pVolBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(280*x,316*y,290*x,316*y+128*y),0,1030);
		m_pVolBar->setToolTipText(L"Music 1 Vol.");
		m_pVolBar->setMin(-256);
		m_pVolBar->setMax(0);
		m_pVolBar->setPos(-256);
		m_pPitchBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(265*x,348*y,270*x,348*y+64*y),0,1040);
		m_pPitchBar->setToolTipText(L"Music 1 freq");
		m_pPitchBar->setMax(-1);
		m_pPitchBar->setMin(-200);
		m_pPitchBar->setPos(-100);

		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(55*x,410*y,145*x,410*y + 16*y), 0, 1000+MUSIC_HPF_BUTTON,
			L"HPF", L"High Pass Filter");
		button->setIsPushButton(true);
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(155*x,410*y,245*x,410*y + 16*y), 0, 1000+MUSIC_LPF_BUTTON,
			L"LPF", L"Low Pass Filter");
		button->setIsPushButton(true);
	}
	else
	{
		m_pFileDisplay = TheEngine::Instance()->GetGUI()->addStaticText(m_wFile.c_str(),rect<s32>(400*x,316*y,580*x,316*y+8*y),true,true,0,2000,true);
		TheEngine::Instance()->GetGUI()->addButton(rect<s32>(415*x,328*y,565*x,328*y + 8*y), 0, 2000+MUSIC_LOAD_BUTTON,
			L"Load", L"Load Music #2");
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(570*x,328*y,590*x,328*y + 8*y),0,2000+MUSIC_MUTE_BUTTON,L"MUTE",L"Mutes track #2");
		button->setIsPushButton(true);
		m_pPlayButton= TheEngine::Instance()->GetGUI()->addButton(rect<s32>(390*x,340*y,590*x,340*y + 16*y), 0, 2000+MUSIC_PLAY_BUTTON,
			L"Play", L"Play/Pause Music #2");
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(415*x,360*y,565*x,360*y + 8*y), 0, 2000+MUSIC_LOOP_BUTTON,
			L"Loop", L"Loop this track");
		button->setIsPushButton(true);
		m_pSongPosition = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(390*x,376*y,590*x,376*y+8*y),0,1010);
		m_pSongPosition->setToolTipText(L"Song position");
		m_pSongPosition->setSmallStep(1);
		m_pSongPosition->setMin(0);
		m_pPanBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(390*x,388*y,590*x,388*y+6*y),0,1020);
		m_pPanBar->setToolTipText(L"Pan");
		m_pPanBar->setMin(-100);
		m_pPanBar->setMax(100);
		m_pPanBar->setPos(0);
		m_pPanBar->setSmallStep(5);
		m_pSpeedBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>(390*x,398*y,590*x,398*y+4*y),0,1020);
		m_pSpeedBar->setToolTipText(L"Playback speed");
		m_pSpeedBar->setMin(1);
		m_pSpeedBar->setMax(200);
		m_pSpeedBar->setPos(100);
		m_pVolBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(350*x,316*y,360*x,316*y+128*y),0,1030);
		m_pVolBar->setToolTipText(L"Music 2 Vol.");
		m_pVolBar->setMin(-256);
		m_pVolBar->setMax(0);
		m_pVolBar->setPos(-256);
		m_pPitchBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>(370*x,348*y,375*x,348*y+64*y),0,1040);
		m_pPitchBar->setToolTipText(L"Music 2 Pitch");
		m_pPitchBar->setMax(-1);
		m_pPitchBar->setMin(-200);
		m_pPitchBar->setPos(-100);

		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(395*x,410*y,485*x,410*y + 16*y), 0, 2000+MUSIC_HPF_BUTTON,
			L"HPF", L"High Pass Filter");
		button->setIsPushButton(true);
		button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>(495*x,410*y,585*x,410*y + 16*y), 0, 2000+MUSIC_LPF_BUTTON,
			L"LPF", L"Low Pass Filter");
		button->setIsPushButton(true);
	}

}

void MusicBoard::Update(float master, bool mute)
{
	if(mute)
	{
		m_pChannel->setMute(true);
	}
	else
	{
		m_pChannel->setMute(m_bMute);
	}
	m_pChannel->isPlaying(&m_bPlaying);
	if(m_bPlaying)
	{
		m_pChannel->getPosition(&m_PlayPos,FMOD_TIMEUNIT_MS);
		int pos = m_pSongPosition->getPos();
		//if((m_PlayPos<=(m_LastPlayPos+TheGame::Instance()->GetDt()*1000)+5000)&&(m_PlayPos>=(m_LastPlayPos+TheGame::Instance()->GetDt()*1000)-5000))
		if(pos==m_LastPlayPos)
		{
			m_pSongPosition->setPos(m_PlayPos);
			m_LastPlayPos=m_PlayPos;
		}
		else
		{
			m_pChannel->setPosition(pos,FMOD_TIMEUNIT_MS);
			m_LastPlayPos=pos;
		}
		
	}
	m_bLastPlaying=m_bPlaying;
	if(!m_bPlaying)
	{
		m_pPlayButton->setText(L"Play");
		m_pSongPosition->setPos(0);
	}
	else if(m_bPaused)
	{
		m_pPlayButton->setText(L"Resume");
	}
	else
	{
		m_pPlayButton->setText(L"Pause");
	}


	if(m_pVolBar->getPos()!=m_LastVol)
	{
		float i  = m_pVolBar->getPos();
		m_LastVol =i;

		float m = m_pVolBar->getMin();
		m /=100; // 1%
		i /= m;
		int d=i;
		wchar_t other[32]= L"Volume: ";
		wchar_t str[20];
		wsprintf(str, L"%d%%", d);
		wcscat(other, str);
		std::wstring v(other);
		m_pVolBar->setToolTipText(v.c_str());
		m_Vol =i/100;	
	}
	m_pChannel->setVolume(m_Vol*master);

	if(m_pSpeedBar->getPos()!=m_LastSpeed)
	{
		float f  = m_pSpeedBar->getPos();
		m_LastSpeed =f;
		f /=100;
		wchar_t other[32]= L"Speed: ";
		wchar_t str[20];
		swprintf(str, L"%3.2f", f);
		wcscat(other, str);
		std::wstring v(other);
		m_pSpeedBar->setToolTipText(v.c_str());

		m_Speed=f;
		if(m_pMusic)
		{
			m_pMusic->setMusicSpeed(m_Speed);
		}
	}
	if(m_pPitchBar->getPos()!=m_LastPitch)
	{
		float f  = -m_pPitchBar->getPos();
		m_LastPitch =f;
		f/=100;
		wchar_t other[32]= L"Pitch modifier: ";
		wchar_t str[20];
		swprintf(str, L"%3.2f", f);
		wcscat(other, str);
		std::wstring v(other);
		m_pPitchBar->setToolTipText(v.c_str());
		m_Pitch=f;
		//m_pChannel->setFrequency(m_Pitch*m_Freq);
		m_pPitchShifter->setParameter(0,m_Pitch);
	}
	if(m_pPanBar->getPos()!=m_LastPan)
	{
		float i  = m_pPanBar->getPos();
		m_LastPan =i;
		i/=100;
		wchar_t other[32]= L"Pan adjustment: ";
		wchar_t str[20];
		swprintf(str, L"%3.1f %%", i);
		wcscat(other, str);
		std::wstring v(other);
		m_pPanBar->setToolTipText(v.c_str());
		m_Pan=i;
		m_pChannel->setPan(i);
	}
}

bool MusicBoard::EventHandler(const SEvent& event)
{
	int id = (event.GUIEvent.Caller->getID()%1000);

	switch(event.GUIEvent.EventType)
	{
	case EGET_FILE_SELECTED:
		m_Path=m_pFileOpen->getFileName();
		if(m_Path!=NULL)
		{
			m_pChannel->stop();
			s32 i = m_Path.findLast('/');
			s32 l = m_Path.size()-i;
			m_wFile = m_Path.subString(i+1,l-1);
			m_pFileDisplay->setText(m_wFile.c_str());
			m_pMusic = TheAudio::Instance()->LoadMusic(m_Path.c_str(),m_pMusic);
			m_bPaused=false;
			m_pChannel->getPosition(&m_PlayPos,FMOD_TIMEUNIT_MS);
			m_pMusic->getLength(&m_MusicLength,FMOD_TIMEUNIT_MS);
			m_pSongPosition->setMax(m_MusicLength);
			m_pSongPosition->setPos(0);
		}
		break;
	case EGET_BUTTON_CLICKED:
		switch(id)
		{
		case MUSIC_MUTE_BUTTON:
			m_bMute=!m_bMute;
			m_pChannel->setMute(m_bMute);
			return true;
		case MUSIC_LOAD_BUTTON:
			m_pFileOpen = TheEngine::Instance()->GetGUI()->addFileOpenDialog(L"Please choose a file.",1,0,2500-(1000*m_bLeft));
			return true;
		case MUSIC_PLAY_BUTTON:
			if(!m_bPlaying)
			{
				m_pChannel = TheAudio::Instance()->PlayMusic(m_pMusic,m_pChannel);

				m_bPaused=false;
				if(m_bLooping)
				{
					m_pChannel->setLoopCount(-1);
				}
				else
				{
					m_pChannel->setLoopCount(0);
				}
				m_pChannel->setMute(m_bMute);
				m_pChannel->setVolume(m_Vol);
				m_pChannel->setPan(m_Pan);
				m_pMusic->setMusicSpeed(m_Speed);
				m_pChannel->getFrequency(&m_Freq);
				//m_pChannel->setFrequency(m_Pitch*m_Freq);
				m_pChannel->addDSP(m_pDsp1, 0);
				m_pChannel->addDSP(m_pDsp2, 0);
				m_pDsp1->setBypass(!m_bHPF);
				m_pDsp2->setBypass(!m_bLPF);
				m_pPitchShifter->setParameter(0,m_Pitch);
				/*m_pPitchBar->setPos(-100);*/
				m_pChannel->addDSP(m_pPitchShifter, 0);
				m_pChannel->setPaused(false);
			}
			else
			{
				bool b;
				m_pChannel->getPaused(&b);
				m_pChannel->setPaused(!b);
				m_bPaused=!b;
			}

			return true;
		case MUSIC_LOOP_BUTTON:
			m_bLooping=!m_bLooping;
			if(m_bLooping)
			{
				m_pChannel->setLoopCount(-1);
			}
			else
			{
				m_pChannel->setLoopCount(0);
			}
			return true;
		case MUSIC_HPF_BUTTON:
			m_bHPF=!m_bHPF;

			m_pDsp1->setBypass(!m_bHPF);
			return true;
		case MUSIC_LPF_BUTTON:
			m_bLPF=!m_bLPF;

			m_pDsp2->setBypass(!m_bLPF);
			return true;
		default:
			return false;
		}
		break;

	default:
		break;
	}
	return false;
}
void MusicBoard::Cleanup()
{
}